package Json;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONException;
import org.json.JSONObject;
import utils.PrintUtil;

import java.util.*;

/**
 * @author Ajie
 * @date 2021/9/24
 * @function
 * Gson
 * jsonArray -> List<Map < String, String>>
 *      [{"0_2ValueName":"0_1value","0_1keyName":"0_1key"},{"1_1keyName":"1_1key","1_2ValueName":"1_1value"}]
 *      ->> new TypeToken<List<Map<String, String>>>() {}.getType()
 *
 *
 *
 * JsonObject -> Map 只是首层。
 * Map<String, Object> map = new JSONObject(jobj).toMap();//官方解法。 内部 entrySet() 遍历
 *
 * map-->json:
 * JSONObject jsonObject = new JSONObject(map);
 *
 */
public class JsonListMapToStr {
    public static void main(String[] args) throws JSONException {

        //jsonArray -> List<Map < String, String>>
        //jsonArrayToListMap();


        mapJsonConvert();
    }

    //json->map, map->json
    private static void mapJsonConvert() throws JSONException {
        String jobj = "{\"avatars\":{\"VISIBLE_LIGHT\":[\"file://null_20220826101447778.jpeg\"],\"NEAR_INFRARED\":[\"file://null_20220826101447778_nir.jpeg\"]},\"deviceInfo\":{\"name\":\"8inch\",\"sn\":\"FF78SL22040003\"},\"extension\":{},\"faceResultType\":\"faceCheckFailure_noBodyTemp_uninvolved\",\"id\":\"edcc881c70794958a5304f05e6d29048\",\"passMode\":\"FACE\",\"rejectReason\":\"FACIAL_COMPARISON_NOT_PASS\",\"resultType\":\"TERMINAL_VALIDATION_FAILURE\",\"score\":\"0.0\",\"syncTime\":1661480087778}\n";
//        String jobj = "";
//        Map<String, Object> map = new JSONObject(jobj).toMap();// 内部 entrySet() 遍历
//        PrintUtil.print(map);
//
//        JSONObject jsonObject = new JSONObject(map);
//        System.out.println("JsonListMapToStr:main:map-->json:"+jsonObject);

        //自己写的遍历
        HashMap hashMap = jsonObjectToMap(jobj);
        PrintUtil.print(hashMap);
        String s = new Gson().toJson(hashMap);//!!! False 不可以。

        // new JSONObject().
        System.out.println("JsonListMapToStr:main: map->json:"+s);
    }

    //JsonObject -> Map 只是首层。
    //官方 Map<String, Object> map = new JSONObject(jobj).toMap();
    @Deprecated
    private static HashMap jsonObjectToMap(String jobj) {
        if (jobj == null || jobj.length()<1) return null;
        try {
            JSONObject jsonObject = new JSONObject(jobj);
            //entrySet() 接口 protect.
            Iterator<String> keys = jsonObject.keys();
            HashMap<String,Object> map = new HashMap<>();//Object: 有 int long ...
            while (keys.hasNext()){
                String next = keys.next();
                map.put(next,jsonObject.get(next));
            }
            return map;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private static void jsonArrayToListMap() {
        //makd jsonArray
        List<HashMap<String, String>> mCachList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put(i + "_1keyName", i + "_1key");
            hashMap.put(i + "_2ValueName", i + "_1value");
            mCachList.add(hashMap);
        }

        Gson gson = new Gson();
        String str = gson.toJson(mCachList);
        System.out.println(str);

        //jsonArray -> list map
        List<Map<String, String>> resultList = new Gson().fromJson(str, new TypeToken<List<Map<String, String>>>() {}.getType());
        for (int i = 0; i < resultList.size(); i++) {
            Map<String, String> map = resultList.get(i);
            for (String key : map.keySet()) {
                System.out.println("key: " + key + "  value: " + map.get(key));
            }
        }
        listToMap(resultList);
    }

    //list 应该递归调用。解决所有的嵌套。后续。。。
    private static Map<String, String> listToMap(List<Map<String, String>> resultList) {
        HashMap<String, String> mapRes = new HashMap<String, String>();
        for (Map<String, String> map : resultList) {
            for (String key : map.keySet()) {
                if (!mapRes.containsKey("visibleLight") && key.contains("visibleLight")) {
                    mapRes.put(map.get(key), map.get("visibleLightValue"));
                    continue;
                }
                if (!mapRes.containsKey("nirKey") && key.contains("nirKey")) {
                    mapRes.put(map.get(key), map.get("nirValue"));
                    continue;
                }
                if (!mapRes.containsKey("switchKey") && key.contains("switchKey")) {
                    mapRes.put(map.get(key), map.get("switchValue"));
                }
            }
        }

        return mapRes;
    }

}
